package com.sojoys.artifact.plugin.id;

import com.jfinal.plugin.IPlugin;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.sojoys.artifact.constant.ConfigConstant;
import com.sojoys.artifact.manager.PropManager;
import com.sojoys.artifact.tools.SnowflakeIdWorker;
import com.sojoys.artifact.tools.ToolError;

/**
 * Twitter_Snowflake<br>
 */
public class SnowflakePlugin implements IPlugin {

	private static SnowflakeIdWorker snowflake = null;
	
	@Override
	public boolean start() {
		int workerId = PropManager.getInt(ConfigConstant.SNOWFLAKEIDWORKER_WORKERID);
		int datacenterId = PropManager.getInt(ConfigConstant.SNOWFLAKEIDWORKER_DATACENTERID);
		snowflake = new SnowflakeIdWorker(workerId, datacenterId);
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}

	public static SnowflakeIdWorker getSnowflake() {
		ToolError.isAndTrue(ArtifactErrorCode.PLUGIN_NOT_INITIALIZED, snowflake == null);
		return snowflake;
	}

	// ==============================Test=============================================
	// /** 测试 */
	// public static void main(String[] args) {
	// SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
	// for (int i = 0; i < 1000; i++) {
	// long id = idWorker.nextId();
	// System.out.println(Long.toBinaryString(id));
	// System.out.println(id);
	// }
	// }

	// public static void main(String[] args) {
	// System.out.println(ToolDate.formatDate("2017-01-01
	// 00:00:00",ToolDate.fmt_yyyy_MM_dd_HH_mm_ss).getTime());
	// }
}